<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){

    	if(session('?username')){
			$this -> error('对不起，您已登陆','/Home/Index/index');
		}else{
			$this->display();
		}
    }
    // 验证账号
    public function auth(){
    	if(IS_POST){
    		$username = I('username');
    		$password = passwd(I('password'));
    		if(M('user')->where("username='%s'",$username)->find() != null){
    			if(M('user')->where("username='%s' and password='%s'",$username,$password)->find() != null){
                    $user = M('user');
                    $user->last_login = time();
                    $user->where("username='%s' and password='%s'",$username,$password)->save();
                    $user_data = $user->where("username='%s' and password='%s'",$username,$password)->find();
    				if(I('remember')=='on'){
		    			session(array(
		    				'username' => $username,
		    				'expire' => 3600000
		    				));
                        session(array('is_admin'=>$user_data['is_admin']));
		    			if(session('?username')){
			    			$this -> success('登陆成功','/Home/Index/index');
			    		}else{
			    			var_dump(session('username'));
			    		}
		    		}else{
		    			session('username',$username);
		    			if(session('?username')){
			    			$this -> success('登陆成功','/Home/Index/index');
			    		}else{
			    			var_dump(session('username',$username));
			    		}
		    		}
    			}
    			else{
    				$this -> error('密码错误！');
    			}
    		}else{
    			$this -> error('没有该用户');
    		}
    	}else{
    		$this -> error('404 NOT FOUND');
    	}
    	
    }
    public function logout(){
    	session(null);
    	if(!session('?username')){
    		$this -> success('登出成功！','/Home/Login/index');
    	}else{
    		$this->error('登出失败');
    	}
    }
}